package com.dk.service.impl;

import cn.hutool.core.io.unit.DataSizeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.model.Work;
import com.dk.mapper.WorkMapper;
import com.dk.service.WorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.WorkVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-17
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {

    @Resource
    private WorkMapper workMapper;

    //模糊查询 编号,名称,单据来源,产品编号,产品名称,客户编码,客户名称,需求日期
    @Override
    public List<WorkVO> getAll(WorkVO workVO) {
        LambdaQueryWrapper<Work> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StringUtils.isNotBlank(workVO.getWorkId()), Work::getWorkId, workVO.getWorkId());//id查询
        wrapper.like(StringUtils.isNotBlank(workVO.getWorkName()), Work::getWorkName, workVO.getWorkName());//名称查询
        wrapper.eq(StringUtils.isNotBlank(workVO.getWorkSource()), Work::getWorkSource, workVO.getWorkSource());//单据来源
        wrapper.like(StringUtils.isNotBlank(workVO.getProductName()), Work::getProductName, workVO.getProductName());//单据来源
        wrapper.eq(StringUtils.isNotBlank(workVO.getCId()), Work::getCId, workVO.getCId());//客户id
        wrapper.like(StringUtils.isNotBlank(workVO.getCName()), Work::getCName, workVO.getCName());//客户name
        /*if (!(new Date() == workVO.getExpireDate())){
           wrapper.eq(Work::getExpireDate,workVO.getExpireDate());
        }*///时间
        List<Work> workList = workMapper.selectList(wrapper);
        List<WorkVO> allJson = getAllJson(workList, "0");
        return allJson;
    }


    //增加方法
    @Override
    public void insertInfoWork(WorkVO workVO) {
        Work work = new Work();
        BeanUtils.copyProperties(workVO, work);
        int insert = workMapper.insert(work);
    }

    //查询
    @Override
    public Work updateByWorkId(String workId) {
        Work work = workMapper.selectById(workId);
        return work;
    }


    @Override
    public void updateInfoWork(WorkVO workVO) {
        Work work = new Work();
        BeanUtils.copyProperties(workVO, work);
        workMapper.updateById(work);
    }

    //传过来的是id并不是pid
    @Override
    public void deleteInfoWork(String workId) {
        //1 创建list集合，用于封装所有删除id值
        List<String> ids = new ArrayList<>();
        //2 向ids集合设置删除菜单id
        deleteInfoWorkJson(ids, workId);
        //把当前id封装到list里面
        ids.add(workId);
        //删除
        workMapper.deleteBatchIds(ids);

    }


    private void deleteInfoWorkJson(List<String> ids, String workId) {
        LambdaQueryWrapper<Work> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Work::getPid, workId);
        wrapper.select(Work::getWorkId);
        List<Work> workList = workMapper.selectList(wrapper);
        //把workList里面id值获取出来，封装ids里面，做递归查询
        workList.stream().forEach(i -> {
            //封装ids里面
            ids.add(i.getWorkId());
            //递归查询
            deleteInfoWorkJson(ids, i.getWorkId());
        });

    }


    private List<WorkVO> getAllJson(List<Work> workList, String pid) {
        List<WorkVO> list = new ArrayList<>();
        for (Work work : workList) {
            WorkVO workVO = null;
            if (pid.equals(work.getPid())) {
                workVO = new WorkVO();
                BeanUtils.copyProperties(work, workVO);
                workVO.setList(getAllJson(workList, work.getWorkId()));
            }
            if (workVO != null) {
                List<WorkVO> list2 = workVO.getList();
                if (list2.size() == 0) {
                    list2.remove(list2);
                }
                list.add(workVO);
            }
        }
        return list;
    }
}
